require(['/js/config.js'], () => {
    require(['request', 'template', 'header', 'footer', 'zoom', 'jq_cookie'], (request, template) => {
        class Goods {
            constructor() {
                this.put_goods_detial().then((resp) => {
                    this.zoom()
                    this.buynow(resp)
                    this.isload(resp)
                })
            }
            put_goods_detial() { //获取商品基本信息
                return new Promise((t) => {
                    request.get_goods().then((resp) => {
                        let hashid = location.hash.slice(1)
                        let imgid = ''
                        if (resp.code === 200) {
                            var arr = resp.body.list.filter((i) => { //返回id是hash的数组
                                return i.id == hashid
                            })
                            $('#goods-out').html(template('goods-in', {
                                arr
                            }))
                        }
                        t(arr[0])
                    })
                })
            }
            zoom() { //放大镜
                $("#zoom_03").elevateZoom({
                    gallery: 'pics', //缩略图id 
                    lensColour: '#fede4f', //放大镜背景颜色 
                    cursor: 'move', //悬浮在放大镜鼠标形状 
                    borderSize: '1', //右侧放大镜边框尺寸 
                    borderColour: '#dddddd' //右侧放大镜边框颜色 
                })
            }
            isload(resp) { //点击加入购物车时判断是否登陆，并记录当前商品
                var _this = this
                $('#goshopping').on('click', function () { //通过ison属性判断
                    if ($.cookie('ison') === undefined) {
                        alert('请先登陆（提醒方式待优化）'); //如果未登录，报错
                    } else { //将数据存入loaclstorage
                        var item = localStorage.getItem($.cookie('ison'))
                        if (item) {
                            var arr = JSON.parse(item)
                        } else {
                            var arr = []
                        }
                        if (arr.some((i) => {
                                if (i.id === resp.id) { //id相同，数量加1
                                    i.num++
                                    return true
                                }
                            }) === false) { //如果没有执行过加1操作
                            arr.push({
                                "id": resp.id,
                                "title": resp.title,
                                "num": 1,
                                "price": resp.price,
                                "color": resp.color,
                                "style": resp.style,
                                "imgs": resp.imgs
                            })
                        }
                        //[{id,名称，数量，单价，颜色，尺码},{}]
                        localStorage.setItem($.cookie('ison'), JSON.stringify(arr))
                        _this.shopnum()
                    }
                })
            }
            shopnum() { //更改购物车数量,点击时加载完成
                var shop = document.querySelector('#shopnumbers'), //只能作为绑定事件
                    sum = 0
                var arr = JSON.parse(localStorage.getItem($.cookie('ison')))
                if (arr) {
                    arr.forEach((i) => {
                        sum += i.num
                    })
                    shop.innerHTML = sum
                }
            }
            buynow(resp) { //立即购买，和isload大同小异，直接选中状态
                var _this = this
                $('#buynow').on('click', function (event) { //通过ison属性判断
                    if ($.cookie('ison') === undefined) {
                        event.preventDefault()
                        alert('请先登陆') //如果未登录，报错
                    } else { //将数据存入loaclstorage
                        var item = localStorage.getItem($.cookie('ison'))
                        if (item) {
                            var arr = JSON.parse(item)
                        } else {
                            var arr = []
                        }
                        if (arr.some((i) => {
                                if (i.id === resp.id) { //id相同，数量加1
                                    i.num++
                                    return true
                                }
                            }) === false) { //如果没有执行过加1操作
                            arr.push({
                                "id": resp.id,
                                "title": resp.title,
                                "num": 1,
                                "price": resp.price,
                                "color": resp.color,
                                "style": resp.style,
                                "ischecked": true,
                                "imgs": resp.imgs
                            })
                        }
                        //[{id,名称，数量，单价，颜色，尺码},{}]
                        localStorage.setItem($.cookie('ison'), JSON.stringify(arr))
                        _this.shopnum()
                    }
                })
            }
        }
        new Goods()
    })
})